<template>
  <div class="admin-layout">
    <!-- 侧边栏 -->
    <div class="sidebar">
      <div class="logo">
        <h1>咖啡管理系统</h1>
      </div>
      <a-menu
        v-model:selectedKeys="selectedKeys"
        mode="inline"
        theme="dark"
      >
        <a-menu-item key="store">
          <router-link to="/admin/store">
            <shop-outlined />
            <span>门店管理</span>
          </router-link>
        </a-menu-item>
        <a-menu-item key="product">
          <router-link to="/admin/product">
            <coffee-outlined />
            <span>商品管理</span>
          </router-link>
        </a-menu-item>
        <a-menu-item key="category">
          <router-link to="/admin/category">
            <appstore-outlined />
            <span>分类管理</span>
          </router-link>
        </a-menu-item>
        <a-menu-item key="param">
          <router-link to="/admin/param">
            <setting-outlined />
            <span>参数管理</span>
          </router-link>
        </a-menu-item>
      </a-menu>
    </div>

    <!-- 主内容区 -->
    <div class="main-content">
      <!-- 顶部导航栏 -->
      <div class="header">
        <div class="header-left">
          <menu-unfold-outlined
            v-if="collapsed"
            class="trigger"
            @click="() => (collapsed = !collapsed)"
          />
          <menu-fold-outlined
            v-else
            class="trigger"
            @click="() => (collapsed = !collapsed)"
          />
        </div>
        <div class="header-right">
          <a-dropdown>
            <a class="ant-dropdown-link" @click.prevent>
              <user-outlined />
              <span class="username">管理员</span>
            </a>
            <template #overlay>
              <a-menu>
                <a-menu-item key="logout" @click="handleLogout">
                  <logout-outlined />
                  <span>退出登录</span>
                </a-menu-item>
              </a-menu>
            </template>
          </a-dropdown>
        </div>
      </div>

      <!-- 内容区 -->
      <div class="content">
        <router-view></router-view>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import {
  MenuUnfoldOutlined,
  MenuFoldOutlined,
  ShopOutlined,
  CoffeeOutlined,
  AppstoreOutlined,
  SettingOutlined,
  UserOutlined,
  LogoutOutlined,
} from '@ant-design/icons-vue';
import { message } from 'ant-design-vue';

const route = useRoute();
const router = useRouter();
const collapsed = ref(false);
const selectedKeys = ref<string[]>([]);

// 根据路由更新选中的菜单项
watch(
  () => route.path,
  (path) => {
    const key = path.split('/')[2];
    selectedKeys.value = [key];
  },
  { immediate: true }
);

// 退出登录
const handleLogout = () => {
  localStorage.removeItem('token');
  message.success('退出成功');
  router.push('/login');
};
</script>

<style scoped>
.admin-layout {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: 200px;
  background-color: #001529;
  transition: all 0.3s;
}

.sidebar.collapsed {
  width: 80px;
}

.logo {
  height: 64px;
  padding: 16px;
  color: #fff;
  text-align: center;
  background-color: #002140;
}

.logo h1 {
  margin: 0;
  font-size: 18px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  background-color: #f0f2f5;
}

.header {
  height: 64px;
  padding: 0 24px;
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}

.header-left {
  display: flex;
  align-items: center;
}

.trigger {
  font-size: 18px;
  cursor: pointer;
  transition: color 0.3s;
}

.trigger:hover {
  color: #1890ff;
}

.header-right {
  display: flex;
  align-items: center;
}

.username {
  margin-left: 8px;
}

.content {
  flex: 1;
  padding: 24px;
  overflow: auto;
}

:deep(.ant-menu-item) a {
  color: rgba(255, 255, 255, 0.65);
}

:deep(.ant-menu-item-selected) a {
  color: #fff;
}

:deep(.ant-menu-dark) {
  background-color: #001529;
}

:deep(.ant-layout-sider-children) {
  display: flex;
  flex-direction: column;
}

:deep(.ant-menu) {
  flex: 1;
}
</style> 